﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SkinnedModel;

namespace FPS
{
    public class ModelAnimation
    {

        AnimationPlayer animationPlayer;

        Dictionary<string, AnimationClip> animationClips;

        public Model Model
        {
            get { return this.model; }
            set 
            {
                this.model = value;
                animationPlayer = new AnimationPlayer(this.model.Tag as SkinningData);
            }
        }
        Model model = null;

        public Vector3 Position
        {
            get { return this.position; }
            set { this.position = value; }
        }
        Vector3 position;

        public Vector3 Rotation
        {
            get { return this.rotation; }
            set { this.rotation = value; }
        }
        Vector3 rotation;

        public float Scale
        {
            get { return this.scale; }
            set { this.scale = value; }
        }
        float scale;


        public ModelAnimation ()
	    {
            position = Vector3.Zero;
            rotation = Vector3.Zero;
            Scale = 1.0f;

	    }

        public static explicit operator Model(ModelAnimation ex)
        {
            return ex.Model;
        }

        public void Set()
        {
            Matrix transform = Matrix.Identity;
            //スケール
            transform *= Matrix.CreateScale(Scale);
            //回転
            transform *= Matrix.CreateRotationX(Rotation.X);
            transform *= Matrix.CreateRotationY(Rotation.Y);
            transform *= Matrix.CreateRotationZ(Rotation.Z);
            //移動
            transform *= Matrix.CreateTranslation(Position);

            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.World = transform;
                }
            }
        }

        public void SetAnimationEnable(params string[] animationName)
        {
            // スキン情報をモデルのTagから取得
            if (this.model == null)
            {
                Console.WriteLine("SetAnimationEnable : モデルが読み込まれていません");
                return;
            }
            SkinningData skinningData = this.model.Tag as SkinningData;

            if (skinningData == null)
                throw new InvalidOperationException
                    ("このモデルのTagにはSkinningDataが設定されていません。\n" +
                        "プロセッサにSkinnedModelProcessorを指定してください");

            // アニメーションプレイヤーの生成と、アニメーションクリップ再生開始
            animationPlayer = new AnimationPlayer(skinningData);
            foreach (var tag in animationName)
            {
                AnimationClip data = skinningData.AnimationClips[tag];
                if (data != null)
                    animationClips[tag] = data;
            }
        }

        public void StartClip(string name)
        {
            animationPlayer.StartClip(animationClips[name]);
        }
    }
}
